﻿using System;
using System.Data;
using System.ServiceModel;
using Cinchoo.Core.Xml.Serialization;
using System.Reflection;
using Cinchoo.Core;
using Cinchoo.Core.Diagnostics;
using System.Data.Common;
using System.Threading;
using System.Collections.Generic;
using Cinchoo.Core.ETL;
using System.Linq;

namespace ChoWorkflowServer.Model
{
    public static class ChoDataModelEvents
    {
        public static event EventHandler<ChoEventArgs<ChoCommandMessage>> OnTaskChanged = delegate { };
        public static event EventHandler<ChoEventArgs<ChoCommandMessage>> OnCalendarChanged = delegate { };
        public static event EventHandler<ChoEventArgs<ChoCommandMessage>> OnUserACLChanged = delegate { };

        public static void RaiseTaskChanged(ChoTaskMgmtCommand cmd, string originHostName, string modifiedBy)
        {
            if (cmd == null) return;
            ChoCommandMessage commandRequest = ChoCommandMessage.New(originHostName, modifiedBy);
            commandRequest.Universalize();
            commandRequest.Command = cmd;

            OnTaskChanged.InvokeEx(null, new ChoEventArgs<ChoCommandMessage>(commandRequest), (del) => OnTaskChanged -= (EventHandler<ChoEventArgs<ChoCommandMessage>>)del);
        }

        public static void RaiseCalendarChanged(ChoCalendarCommand cmd, string originHostName, string modifiedBy)
        {
            if (cmd == null) return;
            ChoCommandMessage commandRequest = ChoCommandMessage.New(originHostName, modifiedBy);
            commandRequest.Universalize();
            commandRequest.Command = cmd;

            OnCalendarChanged.InvokeEx(null, new ChoEventArgs<ChoCommandMessage>(commandRequest), (del) => OnCalendarChanged -= (EventHandler<ChoEventArgs<ChoCommandMessage>>)del);
        }

        public static void RaiseUserACLChanged(ChoUsersACLChangedCommand cmd, string originHostName, string modifiedBy)
        {
            if (cmd == null) return;
            ChoCommandMessage commandRequest = ChoCommandMessage.New(originHostName, modifiedBy);
            commandRequest.Universalize();
            commandRequest.Command = cmd;

            OnUserACLChanged.InvokeEx(null, new ChoEventArgs<ChoCommandMessage>(commandRequest), (del) => OnUserACLChanged -= (EventHandler<ChoEventArgs<ChoCommandMessage>>)del);
        }

        public static void Reset()
        {
            OnTaskChanged = delegate { };
            OnCalendarChanged = delegate { };
            OnUserACLChanged = delegate { };
        }
    }
}
